package com.jf.car.dao.impl;

import com.jf.car.bean.Category;
import com.jf.car.dao.CategoryDao;
import com.jf.car.util.BaseDao;
import com.jf.car.util.MyUtils;
import com.jf.car.util.PageInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CategoryDaoImpl implements CategoryDao {
    @Override
    public List<Category> getAll() {
        String sql = "select * from category";
        return BaseDao.baseQuery(Category.class,sql);
    }

    @Override
    public List<Category> getPage(PageInfo pageInfo, Category category) {
        String sql = "select * from category where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(category.getName())){
            sql+=" and name like concat('%',?,'%') ";
            objs.add(category.getName());
        }
        if(category.getStatus() != null){
            sql+=" and status = ? ";
            objs.add(category.getStatus());
        }
        sql+=" order by id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Category.class,sql,objs);
    }

    @Override
    public int addSelect(Category category) {
        String sql = "insert into category(";
        List<Object> objs = new ArrayList<>();
        if(category.getId() != null){
            sql+=" id,";
        }
        if(MyUtils.isStringNotEmpty(category.getName())){
            sql+=" name,";
        }
        if(MyUtils.isStringNotEmpty(category.getProfile())){
            sql+=" profile,";
        }
        if(category.getStatus() != null){
            sql+=" status,";
        }
        sql = sql.substring(0,sql.lastIndexOf(","));
        sql+=") values(";
        if(category.getId() != null){
            sql+="?,";
            objs.add(category.getId());
        }
        if(MyUtils.isStringNotEmpty(category.getName())){
            sql+="?,";
            objs.add(category.getName());
        }
        if(MyUtils.isStringNotEmpty(category.getProfile())){
            sql+="?,";
            objs.add(category.getProfile());
        }
        if(category.getStatus() != null){
            sql+="?,";
            objs.add(category.getStatus());
        }
        sql = sql.substring(0,sql.lastIndexOf(","));
        sql+=")";
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int upateSelect(Category category) {
        String sql = "update category set ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(category.getName())){
            sql+=" name = ?,";
            objs.add(category.getName());
        }
        if(MyUtils.isStringNotEmpty(category.getProfile())){
            sql+=" profile = ?,";
            objs.add(category.getProfile());
        }
        if(category.getStatus() != null){
            sql+=" status = ?,";
            objs.add(category.getStatus());
        }
        sql = sql.substring(0,sql.lastIndexOf(","));
        sql+=" where id = ?";
        objs.add(category.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int delete(int id) {
        String sql = "delete from category where id = ?";
        return BaseDao.baseUpdate(sql,id);
    }

    @Override
    public int getCount(Category category) {
        String sql = "select count(id) from category where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(category.getName())){
            sql+=" and name = ? ";
            objs.add(category.getName());
        }
        if(category.getStatus() != null){
            sql+=" and status = ? ";
            objs.add(category.getStatus());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public Category getById(int id) {
        String sql = "select * from category where  id = ? ";
        return BaseDao.baseQueryBean(Category.class,sql,id);
    }

    @Override
    public Category getByName(String name) {
        String sql = "select * from category where name = ?";
        return BaseDao.baseQueryBean(Category.class,sql,name);
    }
}
